﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmThemPhieuXuatKho : Form
    {
        public List<NguyenVatLieu_SanPhamDTO> dsnvl;
        public int manguyenvatlieu;
        public frmThemPhieuXuatKho()
        {
            InitializeComponent();
        }

        
        private void frmLapPhieuXuatKho_Load(object sender, EventArgs e)
        {
            try
            {
                dsnvl = new List<NguyenVatLieu_SanPhamDTO>();
                NguyenVatLieuBUS nvlbus = new NguyenVatLieuBUS();
                dsnvl = nvlbus.LayDanhSachNguyenVatLieu();
                cmb_dgv_TenNVL.DataSource = dsnvl;
                cmb_dgv_TenNVL.DisplayMember = "Ten";
                cmb_dgv_TenNVL.ValueMember = "MaNVL";
            }
            catch (Exception)
            {
                //
            }


           

           
        }

       
                        
           


        

        void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            //int selectedIndex = ((ComboBox)sender).SelectedIndex;
            //MessageBox.Show("Selected Index = " + selectedIndex);
            try
            {
                manguyenvatlieu = Convert.ToInt32(((ComboBox)sender).SelectedValue);
                foreach (NguyenVatLieu_SanPhamDTO nvl in dsnvl)
                {
                    if (nvl.MaNVL == manguyenvatlieu)
                    {
                        Double giaban = nvl.DonGiaBan;
                        string donvi = nvl.DonViTinh;
                        int manvl = nvl.MaNVL;

                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[1].Value = manguyenvatlieu.ToString();
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[4].Value = donvi;
                        //dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[5].Value = giaban;

                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[5].Value = giaban;
                    }
                }
            }
            catch (Exception)
            {


            }
           
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dgvChiTiet_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            // tinh thanh tien
            if (e.ColumnIndex == 3)
            {
                try
                {
                    if (dgvChiTiet.Rows[e.RowIndex].Cells[2].Value.ToString() != "" && dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString() != "")
                    {
                        int soluong = Convert.ToInt32(dgvChiTiet.Rows[e.RowIndex].Cells[3].Value.ToString());
                        double dongia = Convert.ToDouble(dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString());
                        double thanhtien = soluong * dongia;
                        dgvChiTiet.Rows[e.RowIndex].Cells[6].Value = thanhtien.ToString();
                    }

                }

                catch (Exception)
                {

                }
            }

            // add stt
            int stt = 1;
            for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
            {
                dgvChiTiet.Rows[i].Cells[0].Value = stt.ToString();
                stt++;
            }


           
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            try
            {
                // them phieu xuat kho
                PhieuXuatKhoBUS pxkbus = new PhieuXuatKhoBUS();
                PhieuXuatKhoDTO pxkdto = new PhieuXuatKhoDTO();
                pxkdto.NgayLap = Convert.ToDateTime(dtpNgayLap.Text);
                pxkdto.MaNV = Convert.ToInt32(txtmanv.Text);
                


                int ID = pxkbus.ThemPhieuXuatKho(pxkdto);

                if (ID == -1)
                {
                    MessageBox.Show("them that bai");
                    return;
                }

                // them chi tiet phieu xuat kho
                string mes = "";
                CT_PhieuXuatKhoBUS ctpxkbus = new CT_PhieuXuatKhoBUS();
                for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
                {
                    try
                    {
                        

                        CT_PhieuXuatKhoDTO ctpxkdto = new CT_PhieuXuatKhoDTO();
                        ctpxkdto.MaPhieuXK = ID;
                        ctpxkdto.MaNVL = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[1].Value);
                        ctpxkdto.SoLuong = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[3].Value);

                        if (ctpxkdto.MaNVL != 0)
                        {
                           mes= ctpxkbus.ThemCTPhieuXuatKho(ctpxkdto);
                        }
                    }
                    catch (Exception)
                    {
                        
                        
                    }
                }

                if (mes != "")
                {
                    MessageBox.Show(mes);
                }
                else
                {
                    MessageBox.Show("them thanh cong");
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            

            

        }

        private void dgvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {
                if (dgvChiTiet.CurrentCell.ColumnIndex == 2)
                {
                    ComboBox cbx = e.Control as ComboBox;


                    if (cbx != null)
                    {
                        //cmb_dgv_TenNVL.DataSource = dsnvl;
                        //cmb_dgv_TenNVL.DisplayMember = "Ten";
                        //cmb_dgv_TenNVL.ValueMember = "MaNVL";

                        cbx.SelectedIndexChanged -= comboBox_SelectedIndexChanged;
                        cbx.SelectedIndexChanged += comboBox_SelectedIndexChanged;
                    }




                }
            }
            catch (Exception)
            {


            }

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            int id = dgvChiTiet.CurrentCell.RowIndex;
            dgvChiTiet.Rows.RemoveAt(id);
        }
    }
}
